﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Wicresoft.SelfService.WorkFlow
{
    public static class Log
    {
        private static Object thisLock = new Object();

        static Log()
        {
        }

        public static void Debug(string task, string message, params object[] objs)
        {
            var entity = new EntityLog
            {
                CreatedTime = DateTime.Now,
                Verb = "Debug",
                TaskName = "[" + Environment.MachineName + "]" + task,
                Message = string.Format(message, objs),
            };

            Push(entity);
        }

        public static void Error(string task, string message, params object[] objs)
        {
            var entity = new EntityLog
            {
                CreatedTime = DateTime.Now,
                Verb = "Error",
                TaskName = "[" + Environment.MachineName + "]" + task,
                Message = string.Format(message, objs),
            };

            Push(entity);
        }

        public static void Exception(string task, string message, params object[] objs)
        {
            var entity = new EntityLog
            {
                CreatedTime = DateTime.Now,
                Verb = "Exception",
                TaskName = "[" + Environment.MachineName + "]" + task,
                Exception = string.Format(message, objs)
            };

            Push(entity);
        }

        public static void Info(string task, string message, params object[] objs)
        {
            var entity = new EntityLog
            {
                CreatedTime = DateTime.Now,
                Verb = "Info",
                TaskName = "[" + Environment.MachineName + "]" + task,
                Message = string.Format(message, objs)
            };

            Push(entity);
        }

        private static void Push(EntityLog entity)
        {
            lock (thisLock)
            {
                File.AppendAllText("system.log", String.Format("CreatedTime: {0} Write log{1}", entity.CreatedTime, Environment.NewLine));
                File.AppendAllText("system.log", String.Format("\tVerb: {0}{1}", entity.Verb, Environment.NewLine));
                File.AppendAllText("system.log", String.Format("\tTaskName: {0}{1}", entity.TaskName, Environment.NewLine));
                File.AppendAllText("system.log", String.Format("\tMessage: {0}{1}", entity.Message, Environment.NewLine));
                File.AppendAllText("system.log", String.Format("\tException: {0}{1}", entity.Exception, Environment.NewLine));
            }
        }
    }

    public class EntityLog
    {
        public DateTime CreatedTime { get; set; }
        public string Verb { get; set; }
        public string TaskName { get; set; }
        public string Message { get; set; }
        public string Exception { get; set; }
    }
}
